# port工具类

# 解析单个端口,原样返回 8080
import re


def __port(port):
    return [port]


# 短横线 8000-8080
def __port_split_line(port):
    port_1 = port.split('-')[-2]  # 8000
    port_2 = port.split('-')[-1]  # 8080
    ans = []
    for i in range(int(port_1), int(port_2 )+ 1):
        ans.append(i)
    return ans


__method_dict = {
    "^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$": __port,
    "^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])-([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$": __port_split_line
}


def __port_split(port):
    ans = []
    for key in __method_dict.keys():
        obj = re.match(key, port)
        if obj:
            method = __method_dict.get(key)
            ans = method(port)
            break
    return ans


def ports_split(ports):
    dic = []
    for port in ports.split(","):
        port = port.strip()
        for x in __port_split(port):
            dic.append(x)
    return list(set(dic))  # 目的是为了去重


# 校验port
def validate_ports(ports):
    for port in ports.split(","):
        port = port.strip()
        if not bool(__port_split(port)):
            # 有一个没值则为出错
            return False
    return True


if __name__ == '__main__':
    print(validate_ports("123,213-214"))
